﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Web.Security;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        private string action = "update";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string user = Session["/UserManagement/ManageUsers.aspxview"].ToString();
                MembershipUser usr = Membership.GetUser(user);
                Guid userGuuid = (Guid)usr.ProviderUserKey;


                StructureFacade userFacade = new StructureFacade();
                User usrObj = userFacade.GetUser(userGuuid);

                txtUserName.Text = usr.UserName;
                txtName.Text = usrObj.Name;
                txtIdNo.Text = usrObj.IdNo;
                txtAddress1.Text = usrObj.Address1;
                txtAddress2.Text = usrObj.Address2;
                txtCity.Text = usrObj.City;
                txtContact.Text = usrObj.Contact;
                txtPostalCode.Text = usrObj.PostalCode.ToString();
                Email.Text = usr.Email;
                drpCountry.SelectedValue = usrObj.Country;
                drpGender.SelectedIndex = usrObj.Gender;
                drpIdType.SelectedValue = usrObj.IdType;

                //usr.ChangePassword(OldPassword.Text, Password.Text);
                //usr.pass
                //usr.ChangePassword(
                //Question.Text = usr.PasswordQuestion;

                //txtPassword.Text = usr.GetPassword();
                //txtPassConfirm.Text = usr.GetPassword();

                drpIdType.SelectedValue = usrObj.IdType;
                drpCountry.SelectedValue = usrObj.Country;
                drpGender.SelectedIndex = usrObj.Gender;  
            }
            


        }

        protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
        {
            User upUsrObj = new User();

            string user = Session["/UserManagement/ManageUsers.aspxview"].ToString();
            MembershipUser usr = Membership.GetUser(user);

            upUsrObj.UserId = (Guid)usr.ProviderUserKey;
            upUsrObj.Name = txtName.Text;
            upUsrObj.IdNo = txtIdNo.Text;
            upUsrObj.Address1 = txtAddress1.Text;
            upUsrObj.Address2 = txtAddress2.Text;
            upUsrObj.City = txtCity.Text;
            upUsrObj.Contact = txtContact.Text;
            upUsrObj.PostalCode =Convert.ToInt32(txtPostalCode.Text);
            usr.Email = Email.Text;
            upUsrObj.IdType = drpIdType.SelectedValue;
            upUsrObj.Gender = Convert.ToByte(drpGender.SelectedIndex);
            upUsrObj.Country = drpCountry.SelectedValue;

            MembershipUser adUser = Membership.GetUser(User.Identity.Name);
            Guid adkey = (Guid)adUser.ProviderUserKey;

            upUsrObj.LastUpBy = adkey;
            upUsrObj.LastUpdateDate = DateTime.Now;

            if (OldPassword.Text != "" || Password.Text != "" )
            {
                usr.ChangePassword(OldPassword.Text, Password.Text);
            }


            StructureFacade usrFac = new StructureFacade();
            usrFac.UpdateUser(upUsrObj);
            
            
        }
    }
}
